package com.bitspilani.infonet.core;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;

import com.bitspilani.infonet.MainActivity;
import com.bitspilani.infonet.bo.LastKnownLocation;
import com.bitspilani.infonet.bo.RiskLocationDetails;
import com.bitspilani.infonet.constants.GlobalConstants;
import com.bitspilani.infonet.serviceinterface.ServiceTask;
import com.google.gson.Gson;

public class AccelerometerHandler implements SensorEventListener {
	private boolean firstSensorValue = true;
	private float lastValueX, lastValueY, lastValueZ;
	private float gravityX = 0, gravityY = 0, gravityZ = 0;
	private double lastPanicBraked = 0;
	private MainActivity mainActivity = new MainActivity();
	private Gson gson = new Gson();

	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSensorChanged(SensorEvent event) {

		if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER){

			float mSensorX = event.values[0] - gravityX;
			float mSensorY = event.values[1] - gravityY;
			float mSensorZ = event.values[2] - gravityZ;//removing the effect of gravity from the sensor

			MainActivity.setAccTextViews("mSensorX: "+mSensorX+", mSensorY: "+mSensorY+", mSensorZ: "+mSensorZ);

			float dx, dy, dz;//deacceleration value across x y and z axis
			if(firstSensorValue){
				lastValueX = mSensorX;
				lastValueY = mSensorY;
				lastValueZ = mSensorZ;
				firstSensorValue = false;
			}else{
				dx = lastValueX - mSensorX;
				dy = lastValueY - mSensorY;
				dz = lastValueZ - mSensorZ;
				if(((dx > GlobalConstants.DECELERATION_THRESHOLD) ||
						(dy> GlobalConstants.DECELERATION_THRESHOLD) ||
						(dz> GlobalConstants.DECELERATION_THRESHOLD))
						&& (System.currentTimeMillis() - lastPanicBraked > GlobalConstants.MIN_BRAKE_DETECTION_INTERVAL)){

					lastPanicBraked = System.currentTimeMillis();

					mainActivity.relaxListning();
					mainActivity.notifyPanicBraking();
					restVariables();
					recordPanicBrake();
					System.out.println(">>>>>> Panic Braked");
					MainActivity.setAccTextViews("PANIC BRAKED: deaccelationX: "+dx
							+", deaccelationY: "+(dy)+", deaccelationZ: "+(dz));

				}else{
					//System.out.println("<NORMAL>"+(lastValueX - mSensorX) +" "+(lastValueY - mSensorY)+" "+(lastValueZ - mSensorZ));
				}

				lastValueX = mSensorX;
				lastValueY = mSensorY;
				lastValueZ = mSensorZ;



			}

		}
//		else if(event.sensor.getType() != Sensor.TYPE_GRAVITY)	{
//		//	System.out.println("GRAVITY>: "+gravityX+" "+gravityY+" "+gravityZ);
//			gravityX= event.values[0];
//			gravityY = event.values[1];
//			gravityZ = event.values[2] ;
//		}
	}
	private void recordPanicBrake() {

	     try {
	    	 LastKnownLocation loc = LocationProvider.getBestKnownLocation();
	    	 RiskLocationDetails riskLocDetails = new RiskLocationDetails();
	    	 
	    	 riskLocDetails.setDeviceId(GlobalConstants.DEVICE_ID);
	    	 riskLocDetails.setVehicleTypeId(GlobalConstants.VEHICLE_TYPE_ID);
	    	 riskLocDetails.setRiskLocation(loc);
	    	 
	    	 String riskLocationJson = gson.toJson(riskLocDetails);
	    	 new ServiceTask().execute(GlobalConstants.STORE_RISK_LOCATION, riskLocationJson);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
		
	}

	private void restVariables(){
		lastValueX = 0;
		lastValueY = 0;
		lastValueZ= 0;
//		gravityX=  0;
//		gravityY  = 0;
//		gravityZ  = 0;
	}


}
